package com.lx.consumer.directreceiver;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import org.apache.logging.log4j.message.MapMessage;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;


import java.io.*;
import java.util.HashMap;
import java.util.Map;


@Component
@RabbitListener(queues = "TestDirectQueue" ,ackMode = "MANUAL")//监听的队列名称 TestDirectQueue
public class DirectReceiver {

        @RabbitHandler
        public void process(Map testMessage,Message message, Channel channel) throws IOException {
            //模拟异常
            long deliveryTag = message.getMessageProperties().getDeliveryTag();
            try {
                File file = new File("example.txt");
                FileInputStream fis = new FileInputStream(file);
                channel.basicReject(deliveryTag, false);
            } catch (IOException e) {
                channel.basicReject(deliveryTag, true);
                throw new RuntimeException(e);
            }finally {

            }
            System.out.println("DirectReceiver第一个消费者收到消息  : " + testMessage.toString());
        }
    }



